import ContextPadProvider from 'bpmn-js/lib/features/context-pad/ContextPadProvider';
import { isAny } from 'bpmn-js/lib/features/modeling/util/ModelingUtil';

class UserTaskContextPadProvider extends ContextPadProvider {
  constructor(
    config,
    injector,
    eventBus,
    contextPad,
    modeling,
    elementFactory,
    connect,
    create,
    popupMenu,
    canvas,
    rules,
    translate,
  ) {
    super(
      config,
      injector,
      eventBus,
      contextPad,
      modeling,
      elementFactory,
      connect,
      create,
      popupMenu,
      canvas,
      rules,
      translate,
      2000,
    );

    this._contextPad = contextPad;
    this._modeling = modeling;
    this._elementFactory = elementFactory;
    this._connect = connect;
    this._create = create;
    this._popupMenu = popupMenu;
    this._canvas = canvas;
    this._rules = rules;
    this._translate = translate;

    this._autoPlace = injector.get('autoPlace', false);
  }

  getContextPadEntries(element) {
    const actions = {};
    if (
      isAny(element.businessObject, [
        'bpmn:EndEvent',
        'bpmn:SequenceFlow',
        'bpmn:DataOutputAssociation',
      ])
    ) {
      return;
    }
    const appendUserTask = (event, element) => {
      const shape = this._elementFactory.createShape({ type: 'bpmn:UserTask' });
      this._create.start(event, shape, {
        source: element,
      });
    };

    const append = this._autoPlace
      ? (event, element) => {
          const shape = this._elementFactory.createShape({ type: 'bpmn:UserTask' });
          this._autoPlace.append(element, shape);
        }
      : appendUserTask;

    // 添加创建用户任务按钮
    actions['append.append-user-task'] = {
      group: 'model',
      className: 'bpmn-icon-user-task',
      title: '用户任务',
      action: {
        dragstart: appendUserTask,
        click: append,
      },
    };

    return actions;
  }
}

export default UserTaskContextPadProvider;
